home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / ascii2fax / str_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  2.8 KB  |  148 lines

  1. /* str_util.c: utility routines for manipulating strings of chars inconjunction with bitmaps */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/str_util.c,v 6.0 1991/12/18 20:15:19 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/str_util.c,v 6.0 1991/12/18 20:15:19 jpo Rel $
  9.  *
  10.  * $Log: str_util.c,v $
  11.  * Revision 6.0  1991/12/18  20:15:19  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include    "fonts.h"
  17.  
  18. static int numTabs(s)
  19. char    *s;
  20. {
  21.         int     ret = 0;
  22.         for (;s && *s != '\0';s++) 
  23.                 if (*s == '\t')
  24.                         ret++;
  25.         return ret;
  26. }
  27.  
  28. StrPtr new_str(f, s, l)
  29. PPFontPtr    f;
  30. char        *s;
  31. int        l;
  32. {
  33.     int i, j, k, tabs;
  34.     StrPtr    ret = (StrPtr) malloc(sizeof(Str));
  35.  
  36.     ret->orig = strdup(s);
  37.     tabs = numTabs(s);
  38.     ret->nchars = l + (6 * tabs); /* 6 because l contains number of tabs */
  39.  
  40.     ret->chars = (CharPtr *) calloc(ret->nchars,
  41.                     sizeof(CharPtr));
  42.     for (i = 0, j = 0;i < l; i++) {
  43.         if (tabs > 0 &&
  44.             s[i] == '\t') {
  45.             tabs--;
  46.             for (k = 0; k < 7; k ++)
  47.                 ret->chars[j++] = f->chars[32]; /* space */
  48.         } else
  49.             ret->chars[j++] = f->chars[(int)s[i]];
  50.     }
  51.     return ret;
  52. }
  53.  
  54. free_str(s)
  55. StrPtr    s;
  56. {
  57.     if (s->chars != NULL)
  58.         free((char *) s->chars);
  59.     if (s->orig)
  60.         free(s->orig);
  61.     free(s);
  62. }
  63.  
  64. static int longestFit(s, maxwid, pw, ph)
  65. StrPtr    s;
  66. int    maxwid, *pw, *ph;
  67. {
  68.     int    tallest = 0, count = 0;
  69.     int    ix;
  70.     *pw = 0;
  71.     *ph = 0;
  72.  
  73.     for (ix = 0; ix < s->nchars; ix++) {
  74.                 if (s->chars[ix] && count + s->chars[ix]->wid <= maxwid) {
  75.                         count += s->chars[ix]->wid;
  76.                         if (s->chars[ix]->ht > tallest)
  77.                                 tallest = s->chars[ix]->ht;
  78.                 } else {
  79.                         ix--;
  80.                         break;
  81.                 }
  82.         }
  83.         *ph = tallest;
  84.         *pw = count;
  85.         return ix;
  86. }
  87.  
  88. static int insertcharbitline(into, x, ch, y)
  89. BitLine    into;
  90. int    x;
  91. CharPtr    ch;
  92. int    y;
  93. {
  94.     int    i;
  95.     for (i = 0; i < ch->wid;i++) {
  96.         if (BL_ISSET(i, ch->bits[y]))
  97.             BL_SET(x+i, into);
  98.         else
  99.             BL_CLR(x+i, into);
  100.     }
  101.     return x+i;
  102. }
  103.         
  104. static void insert_1yline(into, x, s, y, num)
  105. BitLine    into;
  106. int    x;
  107. StrPtr    s;
  108. int    y, num;
  109. {
  110.     int    i;
  111.  
  112.     for (i = 0; i < num; i++)
  113.         x = insertcharbitline(into, x, s->chars[i], y);
  114. }
  115.  
  116. static int insert_str(bm, s, x, y, dx, dy, widest)
  117. BitMap        bm;
  118. StrPtr        s;
  119. int        x, y, *dx, *dy, widest;
  120. {
  121.     int    wid, ht, numInserted, iy;
  122.     
  123.     numInserted = longestFit(s, widest - x, &wid, &ht);
  124.     
  125.     for (iy = 0; iy < ht; iy++)
  126.         insert_1yline(bm[y+iy], x, s, iy, numInserted);
  127.     *dy = ht;
  128.     *dx = wid;
  129.     return numInserted;
  130. }
  131.  
  132. int str_into_bitmap(bm, f, x, y, dx, dy, widest, str, len)
  133. BitMap        bm;
  134. PPFontPtr    f;
  135. int        x, y, *dx, *dy, widest;
  136. char        *str;
  137. int        len;
  138. {
  139.     int    numInserted;
  140.     StrPtr    s = new_str(f, str, len);
  141.     numInserted = insert_str(bm, s, x, y, dx, dy, widest);
  142.     free_str(s);
  143.     return numInserted;
  144. }
  145.  
  146.  
  147.       
  148.